package com.project.weimi.fragment;

import java.util.ArrayList;
import java.util.List;

import com.project.weimi.R;
import com.project.weimi.adapter.CircleAdapter;
import com.project.weimi.adapter.HomeFragmentViewPagerAdapter;
import com.project.weimi.adapter.HotInvitationAdapter;
import com.project.weimi.adapter.TaoTaoAdapter;
import com.project.weimi.entity.response.CircleEntity;
import com.project.weimi.entity.response.HotInvitationEntity;
import com.project.weimi.entity.response.TaoTaoEntity;
import com.project.weimi.view.CircleListView;
import com.project.weimi.view.HotInvitationListView;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.ImageView;

@SuppressLint("InflateParams")
public class LifeFragment extends BaseFragment {

	private ViewPager vp;
	private HotInvitationListView hotInvitation_listView;
	private CircleListView circle_listView;
	// 热帖推荐列表集合
	private List<HotInvitationEntity> hotList;
	// 圈子推荐列表集合
	private List<CircleEntity> circleList;
	// 轮播图下方的圆点
	private View view1, view2, view3, view4, view5, view6;
	// 存放轮播图片的集合
	private List<ImageView> list;
	// isMove手指是否触摸轮播图 isDes此Fragment是否执行onDestory
	private boolean isMove, isDestroy;
	// 轮播图2秒自动轮播
	public static final int TIME_CHANGE = 2000;
	private long desTime = 0;
	
	private CircleListView taotao_listView;
	private List<TaoTaoEntity> taoList;
	private TaoTaoAdapter taoAdapter;
//	private String[] taoTitle = { "我要套套", "变装尤戏", "娘子莫走", "官人来玩", "花式调教" };
	private String[] taoTitle = { "女生悄悄话", "男生撸啊撸"};
	private String[] taoTvs = { "若隐又若现\n被网住的美人\nNET", "清纯女学生\n老师来调教\nSTUDENT" };
	private int[] taoImgs = { R.drawable.tao_item1, R.drawable.tao_item2 };

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			long nowTime = System.currentTimeMillis();
			if (!isDestroy) {
				Message mss = new Message();
				if (!isMove && (nowTime - desTime > TIME_CHANGE)) {
					int i = vp.getCurrentItem();
					vp.setCurrentItem(i + 1);
				}
				handler.sendMessageDelayed(mss, TIME_CHANGE);
			}
		};
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		// 解析布局文件
		View view = inflater.inflate(R.layout.fragment_life, null);

		initView(view);
		initHotInvitationData();
		initCircleData();
		
		
		initTaoListView(view);
		
		return view;
	}
	
	private void initTaoListView(View view) {
		// TODO Auto-generated method stub
		taotao_listView = (CircleListView) view.findViewById(R.id.life_listView);
		taoList = new ArrayList<TaoTaoEntity>();
		List<ImageView> list = null;
		TaoTaoEntity taoEntity = null;
		for (int i = 0; i < taoTitle.length; i++) {
			taoEntity = new TaoTaoEntity();
			taoEntity.setTao_title(taoTitle[i]);
			taoEntity.setTao_tv(taoTvs);
			taoEntity.setTao_iv(taoImgs);

			list = new ArrayList<ImageView>();
			for (int j = 0; j < 2; j++) {
				ImageView img1 = new ImageView(getActivity());
				img1.setImageResource(R.drawable.banner1);
				ImageView img2 = new ImageView(getActivity());
				img2.setImageResource(R.drawable.banner2);
				list.add(img1);
				list.add(img2);
			}
			taoEntity.setImageView(list);
			
			taoList.add(taoEntity);
		}

		taoAdapter = new TaoTaoAdapter(getActivity(), taoList);
		taotao_listView.setAdapter(taoAdapter);
	}

	private void initView(View view) {
		hotInvitation_listView = (HotInvitationListView)view.findViewById(R.id.hotInvitation_listView);
		circle_listView = (CircleListView)view.findViewById(R.id.circle_listView);
		
		view1 = (View) view.findViewById(R.id.view1);
		view2 = (View) view.findViewById(R.id.view2);
		view3 = (View) view.findViewById(R.id.view3);
		view4 = (View) view.findViewById(R.id.view4);
		view5 = (View) view.findViewById(R.id.view5);
		view6 = (View) view.findViewById(R.id.view6);
		vp = (ViewPager) view.findViewById(R.id.vp);
		vp.setLayoutParams(new FrameLayout.LayoutParams(getActivity().getResources().getDisplayMetrics().widthPixels,
				getActivity().getResources().getDisplayMetrics().widthPixels * 188 / 563));
		initViewPager();
	}

	private void initViewPager() {
		list = new ArrayList<ImageView>();
		for (int j = 0; j < 2; j++) {
			ImageView img1 = new ImageView(getActivity());
			img1.setImageResource(R.drawable.banner1);
			ImageView img2 = new ImageView(getActivity());
			img2.setImageResource(R.drawable.banner2);
			ImageView img3 = new ImageView(getActivity());
			img3.setImageResource(R.drawable.banner3);
			ImageView img4 = new ImageView(getActivity());
			img4.setImageResource(R.drawable.banner4);
			ImageView img5 = new ImageView(getActivity());
			img5.setImageResource(R.drawable.banner5);
			list.add(img1);
			list.add(img2);
			list.add(img3);
			list.add(img4);
			list.add(img5);
		}

		Log.i("aaa", "集合长度为======" + list.size());

		// ViewPager适配器
		HomeFragmentViewPagerAdapter adapter = new HomeFragmentViewPagerAdapter(getActivity(), list);
		vp.setAdapter(adapter);
		vp.setCurrentItem(100 * 1000);
		// ViewPager触摸事件
		vp.setOnTouchListener(touchListener);
		// Viewpager滑动监听
		vp.addOnPageChangeListener(pageChangeListener);
	}

	private OnTouchListener touchListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			int i = vp.getCurrentItem();
			int action = event.getAction();
			if (action == MotionEvent.ACTION_MOVE) {
				isMove = true;
				desTime = System.currentTimeMillis();
			}
			if (action == MotionEvent.ACTION_DOWN) {
			}
			if (action == MotionEvent.ACTION_UP) {
				if (!isMove) {
					String stri = i % list.size() + 1 + "条";
					// 点击事件在这里
					// Toast.makeText(getActivity(),
					// stri,Toast.LENGTH_SHORT).show();
				}
				isMove = false;
			}
			if (action == MotionEvent.ACTION_CANCEL) {
				// LogUtils.showTag("MainF", "cancel");
			}
			return false;
		}
	};

	private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {

		@Override
		public void onPageSelected(int arg0) {
			view1.setBackgroundResource(R.drawable.shape_trans_circle);
			view2.setBackgroundResource(R.drawable.shape_trans_circle);
			view3.setBackgroundResource(R.drawable.shape_trans_circle);
			view4.setBackgroundResource(R.drawable.shape_trans_circle);
			view5.setBackgroundResource(R.drawable.shape_trans_circle);

			switch (arg0 % (list.size() / 2)) {
			case 0:
				view1.setBackgroundResource(R.drawable.shape_white_circle);
				break;
			case 1:
				view2.setBackgroundResource(R.drawable.shape_white_circle);
				break;
			case 2:
				view3.setBackgroundResource(R.drawable.shape_white_circle);
				break;
			case 3:
				view4.setBackgroundResource(R.drawable.shape_white_circle);
				break;
			case 4:
				view5.setBackgroundResource(R.drawable.shape_white_circle);
				break;
			default:
				break;
			}
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	};

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if (hidden) {
			// 隐藏后 让轮播图取消轮播
			isDestroy = true;
		} else {
			isDestroy = false;
			handler.removeCallbacksAndMessages(null);
			handler.sendMessageDelayed(new Message(), TIME_CHANGE);
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		isDestroy = true;
		handler.removeCallbacksAndMessages(null);
	}

	@Override
	public void onResume() {
		super.onResume();
		isDestroy = false;
		handler.removeCallbacksAndMessages(null);
		handler.sendMessageDelayed(new Message(), TIME_CHANGE);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		isDestroy = true;
	}
	
	// 热帖推荐初始化数据
	private void initHotInvitationData(){
		hotList = new ArrayList<HotInvitationEntity>();
		for(int i = 0;i < 3;i ++){
			hotList.add(new HotInvitationEntity(R.drawable.a, "肩部不听话吗 学会这几个方法虐爆它", "运动健身馆"));
		}
		
		HotInvitationAdapter hotAdapter = new HotInvitationAdapter(getActivity(),hotList);
		hotInvitation_listView.setAdapter(hotAdapter);
	}
	
	// 圈子推荐初始化数据
	public void initCircleData(){
		circleList = new ArrayList<CircleEntity>();
		for(int i = 0;i < 6;i ++){
			circleList.add(new CircleEntity(R.drawable.a, "COSPLAY秀场", 
					"今日:110", "唯有女人更懂女人，LES天堂美女聚集"));
		}
		
		CircleAdapter adapter = new CircleAdapter(getActivity(),circleList);
		circle_listView.setAdapter(adapter);
	}
	
	
}
